import React from 'react';
import { Switch, Route, Redirect, routerRedux } from 'dva/router';
import dynamic from 'dva/dynamic';
import Dashboard from './routes/Dashboard';

const { ConnectedRouter } = routerRedux;

const RouterConfig = ({ history, app}) => {
    const Error = dynamic({
        app,
        component: () => import('./routes/Error/Error')
    });

    const routes = [
        {
            path: '/home',
            component: () => import('./routes/HomePage/HomePage')
        }, {
            path: '/counter',
            models: () => [
                import('./models/Counter')
            ],
            component: () => import('./routes/Counter/Counter')
        }, {
            path: '/user',
            models: () => [
                import('./models/User')
            ],
            component: () => import('./routes/UserManagement/UserList/index')
        }, {
            path: '/user/:id',
            component: () => import('./routes/UserManagement/Detail')
        }, {
            path: '/menu',
            models: () => [
                import('./models/Menu')
            ],
            component: () => import('./routes/MenuManagement/index')
        }, {
            path: '/permissions',
            models: () => [
                import('./models/Permissions')
            ],
            component: () => import('./routes/PermissionManagement/index')
        },{
            path: '/searchdemo',
            component: () => import('./routes/Page/Search')
        }];

    return (
        <ConnectedRouter history={history}>
            <Dashboard>
                <Switch>
                    <Route exact path="/" render={() => (<Redirect to="/home" />)} />
                    {
                        routes.map(({ path, ...dynamics }, key) => (
                            <Route key={key}
                                   exact
                                   path={path}
                                   component={dynamic({
                                       app,
                                       ...dynamics,
                                   })}
                            />
                        ))
                    }
                    <Route component={Error} />
                </Switch>
            </Dashboard>
        </ConnectedRouter>
    )
};

export default RouterConfig;



